package com.cskaoyan.controller.promote;

import com.cskaoyan.bean.common.BasePageInfoCouponList;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketCoupon;
import com.cskaoyan.bean.po.MarketCouponUser;
import com.cskaoyan.service.promote.CouponService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;

/**
 * @BelongsProject: market-project
 * @BelongsPackage: com.cskaoyan.controller.promote
 * @CreateTime: 2022-11-20  21:14
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("admin/coupon")
public class CouponController {
    @Autowired
    CouponService couponService;

    @RequestMapping("list")
    public BaseRespVo couponList(BasePageInfoCouponList in) {
        PageInfo<MarketCoupon> pageInfo = couponService.selectByBasePageInfo(in);
        CommonData data = CommonData.data(pageInfo);
        return BaseRespVo.succeed(data);
    }

    @RequestMapping("create")
    public BaseRespVo couponCreate(@RequestBody MarketCoupon marketCoupon) throws ParseException {
        Date date = new Timestamp(System.currentTimeMillis());
        marketCoupon.setAddTime(date);
        marketCoupon.setUpdateTime(date);
        couponService.insertByMarketCoupon(marketCoupon);
        return BaseRespVo.succeed(marketCoupon);
    }

    @RequestMapping("update")
    public BaseRespVo couponUpdate(@RequestBody MarketCoupon marketCoupon) {
        couponService.updateByMarketCoupon(marketCoupon);
        return BaseRespVo.succeed(marketCoupon);
    }

    @RequestMapping("delete")
    public BaseRespVo couponDelete(@RequestBody MarketCoupon marketCoupon) {
        couponService.deleteByMarketCoupon(marketCoupon);
        return BaseRespVo.succeed();
    }

    @RequestMapping("read")
    public BaseRespVo couponRead(Integer id) {
        MarketCoupon marketCoupon = couponService.selectById(id);
        return BaseRespVo.succeed(marketCoupon);
    }


    @RequestMapping("listuser")
    public BaseRespVo couponListuser(HttpServletRequest request) {
        PageInfo<MarketCouponUser> list = couponService.selectByBasePageInfoCoupon(request);
        CommonData data = CommonData.data(list);
        return BaseRespVo.succeed(data);
    }


}